package script.groovy

import cn.hutool.core.lang.Console
import cn.hutool.db.Db
import com.data.boot.model.Field2
import com.data.boot.model.Format
import com.data.boot.util.MyBufferReader
import groovy.json.StringEscapeUtils


/**
 * Created by dwk on 2019/6/10.
 * 解析xml
 */
def format(String xmlPath) {
    def xml = new File(xmlPath).text
    def BCPFORMAT = new XmlParser().parseText(xml)
    def RECORDS = BCPFORMAT.RECORD
    println(RECORDS.FIELD.@TERMINATOR)
    Format format = new Format()
    List<Field2> record = new ArrayList<>()
    Map<Integer, String> row = new HashMap<>()
    for (item in BCPFORMAT.RECORD.FIELD) {
        //注意转义字符处理
        record.add(new Field2(item.@ID.toInteger(), StringEscapeUtils.unescapeJava(item.@TERMINATOR)))
    }
    for (item in BCPFORMAT.ROW.COLUMN) {
        row.put(item.@SOURCE.toInteger(), item.@NAME)
    }
    format.row = row
    format.record = record
    format
}


def load(String sql, String path, Format format) {
    List<String[]> rowList = new ArrayList<>()
    new File(path).eachLine {
        line ->
            String[] row = parseLine(line, format)
            rowList.add(row)
            int batch = 10000 //批量值
            if (rowList.size() % batch == 0 && rowList.size() > 0) {//批量插入
                String[][] rows = new String[batch][format.getRow().size()]
                rowList.toArray(rows)
                Db.use().executeBatch(sql, rows)
                rowList.clear()
            }

    }
    if (rowList.size() > 0) {//插入剩余
        String[][] rows = new String[rowList.size()][format.getRow().size()]
        rowList.toArray(rows)
        Db.use().executeBatch(sql, rows)
    }

}

def load2(String sql, String path, Format format) {
    List<String[]> rowList = new ArrayList<>()
    def bufferedReader = new MyBufferReader(new FileReader(path))
    def line
    while ((line = bufferedReader.readLine((char) format.record.last().terminator)) != null) {
        String[] row = parseLine(line, format)
        rowList.add(row)
        int batch = 10000 //批量值
        if (rowList.size() % batch == 0 && rowList.size() > 0) {//批量插入
            String[][] rows = new String[batch][format.row.size()]
            rowList.toArray(rows)
            Db.use().executeBatch(sql, rows)
            rowList.clear()
        }
    }
    if (rowList.size() > 0) {//插入剩余
        String[][] rows = new String[rowList.size()][format.row.size()]
        rowList.toArray(rows)
        Db.use().executeBatch(sql, rows)
    }
}

def parseLine(String line, Format format) {
    def fields = format.record
    def cols = format.row
    List<String> columns = new ArrayList<>()
    for (int i = 0; i < fields.size(); i++) {
        String terminator = fields[i].terminator
        String column
        if (i == fields.size() - 1) {
            column = line//最后一个字段
        } else {
            int j = line.indexOf(terminator) - 1
            if (j < 0) {//分隔符前无内容
                column = ''
            } else {
                column = line[0..j]
            }
            line = line[column.length() + terminator.length()..-1]
        }
        if (cols.containsKey(fields[i].id)) {
            columns.add(column)
        }
    }
    String[] row = new String[columns.size()]
    columns.toArray(row)
}

def buildSql() {
    String sql = 'INSERT INTO od_infoplus_access_log () VALUES (?, ?, ?, ?)'
}

def start() {
    long startTime = System.currentTimeMillis() //获取开始时间
    Db.use().execute('TRUNCATE TABLE test_1')
//    String sql = 'INSERT INTO od_infoplus_access_log VALUES (?, ?, ?, ?, ?, ?)'
    String sql = 'INSERT INTO test_1 VALUES (?, ?, ?, ?)'
//    def format = format(binding.getVariable("xmlPath"))
    def format = format('D:\\IdeaProjects\\data-boot\\src\\main\\resources\\script\\groovy\\2.xml')
//    load(sql, binding.getVariable("path"), format)
    load2(sql, 'F:\\迅雷下载\\workflow.log', format)
    long endTime = System.currentTimeMillis() //获取结束时间
    Console.log("程序运行时间：" + (endTime - startTime) / 1000 + "s")
}

def start2() {
    long startTime = System.currentTimeMillis() //获取开始时间
    Db.use().execute('TRUNCATE TABLE od_infoplus_access_log')
    String sql = 'INSERT INTO od_infoplus_access_log VALUES (?, ?, ?, ?, ?, ?)'
//    def format = format("D:\\IdeaProjects\\data-boot\\src\\main\\resources\\script\\groovy\\1.xml")
    def format = format(args[0])
//    load2(sql, "C:\\Users\\Administrator\\Desktop\\localhost_access_log.2019-05-28.txt", format)
    load2(sql, args[1], format)
    long endTime = System.currentTimeMillis() //获取结束时间
    Console.log("程序运行时间：" + (endTime - startTime) / 1000 + "s")
}

start()